﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IOBusiness;
using System.Data;
using System.Text;

namespace Indochinaodyssey.UserControl.NavigatorMenu
{
    public partial class TreeMenu : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["country"] != null)
                {
                    lblImg.Text="<img alt=\"\" class=\"map\" src=\"../../Style/Tree/map.png\" />";
                    lblTreeMenu.Text = TopMenuCategory(Request.QueryString["country"]);
                    lblCT.Text = TopMenuCountry(Request.QueryString["country"]);
                }  
            }
        }

        private string TopMenuCountry(string countryID)
        {
            string sqlQuocGia = "Select ID,TenQuocGia from tblQuocGia where ID="+countryID;
            clsBSDungChung bs = new clsBSDungChung();
            DataTable tbCountry = new DataTable();
            tbCountry = bs.GetDataTableSQL(sqlQuocGia, "QuocGia");
            return tbCountry.Rows[0][1].ToString().ToUpper();
        }
        private string TopMenuCategory(string countryID)
        {
            string sql = "SELECT t.TenDanhMuc, t.ID,t.TypeID FROM dbo.tblDanhMuc t  where t.ID in(Select l.maDanhmuc from tblTheLoai l where l.maquocgia=" + countryID + " group by l.MaDanhMuc)";
            clsBSDungChung bs = new clsBSDungChung();
            DataTable tbCat = new DataTable();
            tbCat = bs.GetDataTableSQL(sql, "Cate");
            StringBuilder menu = new StringBuilder();


            for (int i = 0; i < tbCat.Rows.Count; i++)
            {
                if (Convert.ToInt32(tbCat.Rows[i][2].ToString()) != 1)
                {
                    menu.Append("<div class=\"root\"><a class=\"link-news2\" href=\"/TourList.aspx?country=" + countryID + "&sub=" + tbCat.Rows[i][1].ToString() + "\" onclick=\"toggleNode(this.parentNode); return false;\">+ " + tbCat.Rows[i][0].ToString() + "</a>");
                }
                else
                {
                    menu.Append("<div class=\"root\"><a class=\"link-news2\" href=\"/Infomation.aspx?country=" + countryID + "&loaiid=" + tbCat.Rows[i][1].ToString() + "\" onclick=\"toggleNode(this.parentNode); return false;\">+ " + tbCat.Rows[i][0].ToString() + "</a>");
                }
                menu.Append(TopMenuSubCategory(countryID, tbCat.Rows[i][1].ToString(), tbCat.Rows[i][2].ToString()));
               
                menu.Append("</div>");
            }

            return menu.ToString();
        }
        private string TopMenuSubCategory(string countryID, string catID,string type)
        {
            string sql = "Select ID,TenTheLoai from tblTheLoai where MaDanhMuc=" + catID + " and MaQuocGia=" + countryID;
            string sql2 = "Select t.tieude,t.id from tblInfomation t,tblTheLoai tt,tblDanhMuc d where t.TheLoaiID=tt.Id and tt.madanhmuc=d.id and tt.maquocgia=" + countryID;
            clsBSDungChung bs = new clsBSDungChung();
            DataTable tbCat = new DataTable();
            DataTable tbInfo = new DataTable();
            tbCat = bs.GetDataTableSQL(sql, "SubCategory");
            tbInfo = bs.GetDataTableSQL(sql2, "Info");
            StringBuilder menu = new StringBuilder();

            if (type == "1")
            {
                for (int i = 0; i < tbInfo.Rows.Count; i++)
                {
                    menu.Append("<div><a class=\"link-news\" href=\"/InfomationDetail.aspx?country=" + countryID + "&sub=" + catID + "&id=" + tbInfo.Rows[i][1].ToString() + "\">- " + tbInfo.Rows[i][0].ToString() + "</a></div>");
                }
            }
            else if (type=="2")
            {
                for (int i = 0; i < tbCat.Rows.Count; i++)
                {
                    menu.Append("<div><a class=\"link-news\" href=\"/Tour.aspx?country=" + countryID + "&sub=" + catID + "&cateid=" + tbCat.Rows[i][0].ToString() + " \">- " + tbCat.Rows[i][1].ToString() + "</a></div>");

                }
            }
            return menu.ToString();
        }
    }
}